package com.example.operation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.operation.domin.DecSpecs;
import com.example.operation.domin.DecWares;
import com.example.operation.dto.DecSpecsDto;
import com.example.operation.dto.DecWaresDto;
import com.example.operation.mapper.DecSpecsMapper;
import com.example.operation.mapper.DecWaresMapper;
import com.example.operation.service.DecWaresService;
import com.example.operation.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: DecWaresServiceImpl
 * @Description:
 * @Author
 * @Date 2021/11/24
 * @Version 1.0
 */

@Service
public class DecWaresServiceImpl extends ServiceImpl<DecWaresMapper, DecWares> implements DecWaresService{

    @Autowired
    private DecWaresMapper decWaresMapper;

    @Autowired
    private DecSpecsMapper decSpecsMapper;


    @Override
    public Page<DecWaresVo> page(Integer pageCurrent, Integer pageSize, Integer pageNumber, String title, Integer offShelf) {
        //创建分页对象
        Page<DecWaresVo> page = new Page<>(pageCurrent,pageSize,pageNumber);
        //获取分页信息
        Page<DecWaresVo> decWaresVoPage = decWaresMapper.page(page,title,offShelf);
        //集合所有分页信息
        List<DecWaresVo> decWaresVos =decWaresVoPage.getRecords();
        //遍历
        for (DecWaresVo decWaresVo : decWaresVos) {
            //根据id放入对应子表信息
            Integer id = decWaresVo.getId();
            List<DecSpecsVo> decSpecsVos = decWaresMapper.dsVoList(id);
            decWaresVo.setDecSpecsVos(decSpecsVos);
        }
        return decWaresVoPage;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(DecWaresDto decWaresDto) {

        DecWares decWares = new DecWares();
        LambdaQueryWrapper<DecWares> eq = new QueryWrapper<DecWares>()
                .lambda().eq(DecWares::getTitle,decWaresDto.getTitle());
        Integer count = decWaresMapper.selectCount(eq);
        if(count > 0){
            throw new RuntimeException("商品标题不能重复");
        }
        BeanUtils.copyProperties(decWaresDto,decWares);
        this.save(decWares);
        //商品规格删除已存在的
        LambdaQueryWrapper<DecSpecs> eq1 = new QueryWrapper<DecSpecs>()
                .lambda().eq(DecSpecs::getWaresId,decWaresDto.getId());
        Integer count1 = decSpecsMapper.selectCount(eq1);
        if(count1 > 0){
            decSpecsMapper.delete(eq1);
        }else if(count1 == 0){
            throw new RuntimeException("当前商品规格不存在，无法删除");
        }
        //商品规格添加
        DecSpecsDto decSpecsDto = decWaresDto.getDecSpecsDtos();
        if(decSpecsDto != null){
            Integer waresId = this.getOne(eq).getId();
            DecSpecs decSpecs = new DecSpecs();
            BeanUtils.copyProperties(decSpecsDto,decSpecs);
            decSpecs.setWaresId(waresId);
            decSpecs.setSpecs(decSpecsDto.getSpecs());
            decSpecs.setSellPrice(decSpecsDto.getSellPrice());
            decSpecs.setCostPrice(decSpecsDto.getCostPrice());
            decSpecs.setStock(decSpecsDto.getStock());
            decSpecs.setWeight(decSpecsDto.getWeight());
            decSpecs.setSpecsCode(decSpecsDto.getSpecsCode());
            decSpecsMapper.insert(decSpecs);
        }

    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(DecWaresDto decWaresDto) {
        DecWares decWares = new DecWares();
        LambdaUpdateWrapper<DecWares> eq = new UpdateWrapper<DecWares>()
                .lambda().eq(DecWares::getTitle, decWaresDto.getTitle());
        Integer count = decWaresMapper.selectCount(eq);
        if (count == 0) {
            throw new RuntimeException("您输入的商品标题不存在");
        }
        BeanUtils.copyProperties(decWaresDto, decWares);
        this.updateById(decWares);
        //商品规格删除已存在的
        LambdaQueryWrapper<DecSpecs> eq1 = new QueryWrapper<DecSpecs>()
                .lambda().eq(DecSpecs::getWaresId, decWaresDto.getId());
        Integer count1 = decSpecsMapper.selectCount(eq1);
        if (count1 > 0) {
            decSpecsMapper.delete(eq1);
        } else if (count1 == 0) {
            throw new RuntimeException("当前商品规格不存在，无法删除");
        }

        //商品规格添加
        DecSpecsDto decSpecsDto = decWaresDto.getDecSpecsDtos();
        if (decSpecsDto != null) {
            Integer waresId = this.getOne(eq).getId();
            DecSpecs decSpecs = new DecSpecs();
            BeanUtils.copyProperties(decSpecsDto, decSpecs);
            decSpecs.setWaresId(waresId);
            decSpecs.setSpecs(decSpecsDto.getSpecs());
            decSpecs.setSellPrice(decSpecsDto.getSellPrice());
            decSpecs.setCostPrice(decSpecsDto.getCostPrice());
            decSpecs.setStock(decSpecsDto.getStock());
            decSpecs.setWeight(decSpecsDto.getWeight());
            decSpecs.setSpecsCode(decSpecsDto.getSpecsCode());
            decSpecsMapper.insert(decSpecs);
        }

    }

    @Override
    public List<DecWaresVo> selectDwVo(Integer id) {
        List<DecWaresVo> decWaresVos = decWaresMapper.selectDwVo(id);
        //遍历
        for (DecWaresVo decWaresVo : decWaresVos) {
            //根据id放入对应子表信息
            Integer id1 = decWaresVo.getId();
            List<DecSpecsVo> decSpecsVos = decWaresMapper.dsVoList(id1);
            List<DispatchCityVo> cityVos = decWaresMapper.city(id1);
            List<DispatchExpressVo> expresses = decWaresMapper.express(id1);
            List<DispatchConditionVo> dispatchConditionVos = decWaresMapper.condtionVo(id1);
            decWaresVo.setDecSpecsVos(decSpecsVos);
            decWaresVo.setDispatchCityVos(cityVos);
            decWaresVo.setDispatchExpressVos(expresses);
            decWaresVo.setDispatchConditionVos(dispatchConditionVos);

        }
        return decWaresVos;

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateBatchById(List<Integer> list, Integer offShelf) {
        List<DecWares> list1 = new ArrayList<>();
        if(offShelf == 1) {
            for (Integer id : list) {
                DecWares decWares = this.getById(id);
                decWares.setOffShelf(0);
                list1.add(decWares);
            }
        }
        else if (offShelf == 0){
            for (Integer id : list) {
                DecWares decWares = this.getById(id);
                decWares.setOffShelf(1);
                list1.add(decWares);
            }
        }
        else {
            throw new RuntimeException("上下架格式错误，请确认操作正确后再试");
        }
        this.updateBatchById(list1);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeByIds1(List<Integer> idList) {
        this.removeByIds(idList);
        for (Integer id : idList) {
            //商品规格删除已存在的
            LambdaQueryWrapper<DecSpecs> eq = new QueryWrapper<DecSpecs>()
                    .lambda().eq(DecSpecs::getWaresId, id);
            Integer count = decSpecsMapper.selectCount(eq);
            if (count > 0) {
                decSpecsMapper.delete(eq);
            } else if (count == 0) {
                throw new RuntimeException("当前商品规格不存在，无法删除");
            }


        }
    }
}
